package com.ep.general.kursant;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.ep.general.GeneralClassMdt;

public class KursantMdt extends GeneralClassMdt<Kursant> {

	public KursantMdt() {
		super(Kursant.class);
	}

	@Override
	public Kursant get(int id) {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Kursant.class);
		crit.add(Restrictions.eq("id", id));
		return uniqueResult(crit);
	}

	public Kursant getByEmail(String email) {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Kursant.class);
		crit.add(Restrictions.eq("email", email));
		return uniqueResult(crit);
	}

	public Kursant get(String login, String haslo) {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Kursant.class);
		crit.add(Restrictions.eq("login", login));
		crit.add(Restrictions.eq("haslo", haslo));

		return uniqueResult(crit);
	}

	public Kursant get(String login) {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Kursant.class);
		crit.add(Restrictions.eq("login", login));

		return uniqueResult(crit);
	}

	public boolean isLoginUnique(String login) {
		getSession().beginTransaction();
		Criteria criteria = getSession().createCriteria(Kursant.class).add(Restrictions.eq("login", login));
		return list(criteria).size() == 0;
	}

	public boolean isEmailUnique(String email) {
		getSession().beginTransaction();
		Criteria criteria = getSession().createCriteria(Kursant.class).add(Restrictions.eq("email", email));
		return list(criteria).size() == 0;
	}

	public List<Kursant> getList() {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Kursant.class);
		return list(crit);
	}

	public List<Kursant> getNieprzypisani() {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Kursant.class);
		crit.add(Restrictions.isNull("instruktor"));
		return list(crit);
	}

	public boolean isPrzypisany(int kursantId) {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Kursant.class);
		crit.add(Restrictions.eq("id", kursantId));
		crit.add(Restrictions.isNull("instruktor"));
		return uniqueResult(crit) != null;
	}

}
